<template>
  <div class="components-container">
    <div class="header-form" style="margin-bottom:0;">
        <div class="layui-inline">
          <label class="layui-form-label" >车牌号</label>
            <!-- <div class="layui-input-inline">
              <el-input v-model="selectData.carLicLike" placeholder="请输入车牌号"></el-input>
            </div> -->
            <div class="layui-input-inline" style="width:185px;">
                <el-cascader :options="carnum" v-model="carLicLikes" filterable :show-all-levels="false" @change="btnSearch">
                </el-cascader>
              </div>
        </div>
      
         <div class="layui-inline">
          <label class="layui-form-label" >驾驶员</label>         
          <div class="layui-input-inline" style="width:185px;">
            <el-select v-model="selectData.cdhDricerLike" @change="btnSearch">
              <el-option label="不选择" :value="null"></el-option>
              <el-option v-for="item in drivers" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </div>
        </div>
                 <div class="layui-inline" v-if="isShowcarrier">
          <label class="layui-form-label" >接运员</label>         
          <div class="layui-input-inline" style="width:185px;">
            <el-select v-model="selectData.StanameOrLike" @change="btnSearch">
              <el-option label="不选择" :value="null"></el-option>
              <el-option v-for="item in carrier" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </div>
        </div>
        <div class="layui-inline">
          <label class="layui-form-label" >派车地点</label>
          <div class="layui-input-inline">
              <el-input v-model="selectData.cdhadressLike" placeholder="派车地点" @keydown.enter.native="btnSearch"></el-input>
          </div>
        </div>
    </div>
    <div class="header-form" style="margin-top:10px;">
        <div class="layui-inline">
            <label class="layui-form-label">出车类型</label>
            <div class="layui-input-inline" style="width:185px;">
              <el-select v-model="selectData.CdhcodnameEquals" placeholder="出车类型" @change="btnSearch">
                <el-option v-for="item in cdhTypes" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label" >出车日期</label>
            <div class="layui-input-inline"> 
              <el-date-picker v-date v-model="selectData.CdhactualdateGreaterAndEquals" type="date" placeholder="请选择日期大于该日期" @change="btnSearch">
              </el-date-picker>
            </div>
            <div class="layui-input-inline">
              <el-date-picker v-date v-model="selectData.CdhactualdateLessAndEquals" type="date" placeholder="请选择日期小于该日期" @change="btnSearch">
              </el-date-picker>
            </div>
        </div>
      <el-button type="primary" @click="btnSearch" :loading="this.$store.state.isloading">搜索</el-button>
      <el-button  @click="btnRepeat">重置</el-button>
    </div>
    <!-- 功能列表 -->
    <div style="margin-top:10px;">
      <ul class="toolList">
        <li><el-button type="primary" @click="btnSearch" :loading="this.$store.state.isloading">刷新</el-button></li>
        <li v-if="btnShow(item.BTNVALUE)" v-for="(item, index) in $store.state.btnListCommon" @click="operate(item.BTNVALUE)">
            <el-button type="primary" :value="item.BTNSHOWNAME" v-text="item.BTNSHOWNAME"></el-button>
        </li>
      </ul>
    </div>
    <!-- 数据列表 -->
    <dataTable :height="tableHeight" :isIndex="true" :hiddenPageOrder="false" :headList="headList" :dataList="dataList" :pageOrder="pageOrder" @pageChange="pageChange" @selectRow="selectRow"></dataTable>
    <h1>{{message}}</h1>
    <!-- 派车信息 详情-->
    <sweet-modal ref="dialog" class="dialog">
      <h3>{{title}}</h3>
      <div style=" height:300px; width:700px; margin:30px 50px">
        <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label">出车类型</label>
              <div class="layui-input-inline">
                <el-input v-model="cdhdata.CDHCODNAME" readonly></el-input>
              </div>
            </div>

            <div class="layui-inline">
              <label class="layui-form-label">车辆类型</label>
              <div class="layui-input-inline">
                <el-input v-model="cdhdata.CARCODNAME" readonly></el-input>
              </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label">车牌号</label>
              <div class="layui-input-inline">
                <el-input v-model="cdhdata.CARLIC" readonly></el-input>
              </div>
            </div>
    
            <div class="layui-inline">
              <label class="layui-form-label">驾驶员</label>
              <div class="layui-input-inline">
                <el-input v-model="cdhdata.STANAME" readonly placeholder="司机"></el-input>
              </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label">接运员</label>
              <div class="layui-input-inline">
                <el-input v-model="cdhdata.CarryName" readonly ></el-input>
              </div>
            </div>

            <div class="layui-inline">
              <label class="layui-form-label">统计</label>
              <div class="layui-input-inline">
                <el-input v-model="cdhdata.CensName"  readonly></el-input>
              </div>
            </div>
        </div>
        <div class="layui-form-item">
          <div class="layui-inline">
            <label class="layui-form-label">出车时间</label>
            <div class="layui-input-inline">
              <el-date-picker type="datetime"  v-model="sendCarTime"  readonly></el-date-picker>
            </div>
          </div>
          <div class="layui-inline">
              <label class="layui-form-label">里程</label>
              <div class="layui-input-inline">
                <el-input v-model="cdhdata.CDHMILEAGE" placeholder="里程" readonly></el-input>
              </div>
          </div>
        </div>
        <div class="layui-form-item">
          <label class="layui-form-label">出车地址</label>
          <div class="layui-input-inline">
            <el-input v-model="cdhdata.CDHADDRESS"  readonly style="width:480px"></el-input>
          </div>
        </div>
               
        <div class="layui-form-item">
          <label class="layui-form-label" >备注</label>
          <div class="layui-input-inline" >
            <el-input type="textarea"  style="width:480px" v-model="cdhdata.CDHREMARKS" readonly></el-input>
          </div>
        </div>
      </div>
      <div class="layui-form-item btnCenter">
        <el-button @click="cancel">取消</el-button>
      </div>
    </sweet-modal>
     <sweet-modal ref="dialog1" class="dialog" :sweetModalMaxWidth="pageWidth">
      <h3>打印火化证明</h3>
      <iframe :src="pdfUrl" style="width:1150px; height:500px;margin-top:20px;"></iframe>
    </sweet-modal>
    <carSendMain ref="sendMain" :orderType="order" @refreshData="getData"></carSendMain>
    <carSendNewMain ref="carSendNewMain" :orderType="order" @refreshData="getData"></carSendNewMain>

  </div>
</template>

<script>
import carSendMain from "./carSendMain";
import carSendNewMain from "./carSendNewMain";
import sweetModal from "@/components/sweetModal";
import apiBase from "@/api/apiBase.js";
import dataTable from "@/components/Table/DataTable";
import { formatDate, timeStringToDate } from "@/utils/date.js";
import { manage } from "@/mixins/managePage.js";

const INFO = "详情";
const CHANGE = "修改";
const COPY = "打印";
const DAOCHU = "导出";
export default {
  name: "carSendHistory",
  mixins: [manage],
  data() {
    return {
      tableHeight:'600',
      //查询条件
      selectData: {
        CdhactualdateGreaterAndEquals: null,
        CdhactualdateLessAndEquals: null,
        CdhcodnameEquals: null
      },
      sendCarTime:null,
      cdhdata: {},
      cdhTypes: [],
      drivers:[],
      carrier:[], //宁波 接运员下拉接收
      carnum: [],
      carLicLikes: [],
      title: null,
      message:null,
      pdfUrl: null,
      pageWidth: 1250,
      maxHeight: 550,
      cdhTypes: [],
      isShow: null,
      Kilometer: 0,
      OutDataExcel:[],
      isShowcarrier:null, //宁波个性化 控制接运员显隐
    };
  },
  methods: {
    //功能操作
    operate(order) {
      this.order = order;
      switch (this.order) {
        case INFO:
          alert('不可查看详情')
          break;
        case "派车修改": //宁波派车修改显示登记派车页面
          this.$refs.carSendNewMain.open(this.order, this.currentRow.BUSSNO);
          break;
        case CHANGE:
          this.$refs.sendMain.operateCdh(this.order,this.currentRow.CDHID);
          break;
        case COPY:
          this.copycarhz();
          break;
        case DAOCHU:
          this.exportExcel();
          break;
        default:
      }
    },
    //导出excel
    exportExcel(){
      var startTime;
      var endTime;
      if(this.selectData.CdhactualdateGreaterAndEquals){
        startTime = this.selectData.CdhactualdateGreaterAndEquals.toLocaleDateString();
      }else{
        startTime = '空';
      }
      if(this.selectData.CdhactualdateLessAndEquals){
        endTime = this.selectData.CdhactualdateLessAndEquals.toLocaleDateString();
      }else{
        endTime = '空';
      }
      var header = '出车汇总' + '(' + startTime + '--' + endTime + ')';
       if (this.dataList.length === 0){
          this.$message.error("无导出数据！");
          return;
      }
      this.downloadLoading = true;
      // this.OutDataExcel.push({CDHSTATUSDESC:"合计",CDHMILEAGE:this.Kilometer})
      require.ensure([], () => {
        const { export_json_to_excel } = require("@/vendor/Export2Excel");
        export_json_to_excel(this.headList, this.OutDataExcel, header);
        this.downloadLoading = false;
      });
    },
    //打印出车汇总
    copycarhz(){
      let Data=JSON.parse(JSON.stringify(this.selectData))
          Data.CdhactualdateGreaterAndEquals= formatDate(Data.CdhactualdateGreaterAndEquals,"yyyyMMdd");
          Data.CdhactualdateLessAndEquals= formatDate(Data.CdhactualdateLessAndEquals,"yyyyMMdd");
          if(this.carLicLikes.length != 0){
            Data.carLicLike = this.carLicLikes[1];
          }else{
            Data.carLicLikes = null;
          }
      let data = {
         Data:Data
      };
      apiBase.DoService(
        data,
        apiBase.getApiAdress().Pdf.PrintCollectionofcars,
        res => {
          this.pdfUrl = res.data.content.PDFNAME;
          this.$refs.dialog1.open();
      });
    },
    //获取表头
    getSendCarColumns() {
      let data = {};
      apiBase.DoService(
        data,
        apiBase.getApiAdress().Column.GetSendCarRecordColumns,
        res => {
          this.headList = res.data.content;
          // if(this.isShow === true){
          //   this.headList.splice(9,1)
          // }
        }
      );
    },
    btnShow(val){
      if(val == CHANGE && this.currentRow){
        return true;
      }
      if(val == '派车修改' && this.currentRow){
        return true;
      }
      if(val == COPY) return true;
      if(val == DAOCHU) return true;
    },
    // 获取车辆信息
    getcarnum(){
      let data = {};
      apiBase.DoService(
        data,
        apiBase.getApiAdress().Car.QueryCarByType,
        res => {
          let cardata = res.data.content;
          let legendsCarData = cardata.map(item => ({
            label: item.CodeName,
            value: item.CodeValue,
            children: item.list_Cars.map(caritem => ({
              label: caritem.CARLIC,
              value: caritem.CARLIC
            }))
          }));
          this.carnum = legendsCarData;
        }
      );
    },
     //获取派车类别
    getSendCdhTypes() {
    let data = {};
                apiBase.DoService(
                    data,
                    apiBase.getApiAdress().Code.GetSendCdhType,
                    res => {
                        let cdhTypeData = res.data.content,
                            legendsCdhData = cdhTypeData.map(item => ({
                                label: item.CodeName,
                                value: item.CodeValue
                            }));
                        this.cdhTypes = legendsCdhData;
                    }
                );
            },
    //获取派车信息
    getData() {
      let Data=JSON.parse(JSON.stringify(this.selectData))
          Data.CdhactualdateGreaterAndEquals= formatDate(Data.CdhactualdateGreaterAndEquals,"yyyyMMdd");
          Data.CdhactualdateLessAndEquals= formatDate(Data.CdhactualdateLessAndEquals,"yyyyMMdd");
          if(this.carLicLikes.length != 0){
            Data.carLicLike = this.carLicLikes[1];
          }else{
            Data.carLicLikes = null;
          }
      let data = {
         PageIndex:-1,
         PageSize: 0,
        //  PageSize:200,
         OrderBy:this.pageOrder.orderType,
         OrderByType:this.pageOrder.orderSort,
         Data:Data
      };
      apiBase.DoService(data, apiBase.getApiAdress().Sendcar.Query, res => {
        this.dataList = res.data.content.OutData;
        this.OutDataExcel = res.data.content.OutData;
        this.message=res.data.content.Message;
        this.pageOrder.totalNum = res.data.content.RecordCount;  
        let numgl = 0;
        for(var i in this.dataList){
          var s = Number(this.dataList[i].CDHMILEAGE);
          numgl = s + numgl 
        }
        this.Kilometer = numgl;
        this.OutDataExcel.push({CDHSTATUSDESC:"合计",CDHMILEAGE:this.Kilometer})
      });
    },
    //获取派车类别
    getSendCdhTypes() {
      let data = {};
      apiBase.DoService(
        data,
        apiBase.getApiAdress().Code.GetSendCdhType,
        res => {
          let cdhTypeData = res.data.content,
            legendsCdhData = cdhTypeData.map(item => ({
              label: item.CodeName,
              value: item.CodeValue
            }));
          this.cdhTypes = legendsCdhData;
        }
      );
    },
 
    //派车管理 搜索框重置
    btnRepeat() {
      this.selectData = {
        CdhactualdateGreaterAndEquals: null,
        CdhactualdateLessAndEquals: null,
        CdhcodnameEquals: null
      };
      this.carLicLikes = [];
      this.selectData.CdhactualdateGreaterAndEquals = new Date();
      this.selectData.CdhactualdateLessAndEquals = new Date();
      this.getData();
    },
    //获取司机
    getDrivers() {
      let data = {};
      apiBase.DoService(data, apiBase.getApiAdress().Staff.QueryDriver, res => {
        let driverData = res.data.content,
          legendsDrivers = driverData.map(item => ({
            label: item.STANAME,
            value: item.STAID
          }));
        this.drivers = legendsDrivers;
      });
    },
    //宁波个性化 控制显隐
     getningbo() {
  
     
    },
    //获取接运员 --宁波
    getcarrier() {
      let data = {};
      apiBase.DoService(data, apiBase.getApiAdress().Staff.QueryCarry, res => {
        let carrierData = res.data.content,
          legendscarrier = carrierData.map(item => ({
            label: item.STANAME,
            value: item.STAID
          }));
        this.carrier = legendscarrier;
      });
    },
    //详情 --派车
    dataDetail() {
      var data = {
        CDHID: this.currentRow.CDHID
      };
      apiBase.DoService(data, apiBase.getApiAdress().Sendcar.Detail, res => {
        this.cdhdata =res.data.content;
        this.sendCarTime = timeStringToDate(this.cdhdata.CDHACTUALDATE,this.cdhdata.CDHACTUALTIME);
       });
    }
  },
  components: {
    sweetModal,
    dataTable,
    carSendMain,
    carSendNewMain
  },
  created() {
    this.selectData.CdhactualdateGreaterAndEquals=new Date();
    this.selectData.CdhactualdateLessAndEquals = new Date();
    // this.selectData.CdhactualdateGreaterAndEquals.setDate(1);
    apiBase.DoService(
      {},
      apiBase.getApiAdress().Param.GetIsShowOutOfTime,
      res => {
        this.isShow = res.data.content
        //获取表头
        this.getSendCarColumns();
      });
    //派车管理 列表
    this.getData();
    //派车类型
    this.getSendCdhTypes();
    this.getDrivers();
    this.getcarnum();
      apiBase.DoService({}, apiBase.getApiAdress().Param.GetIsShowSendCarCDF, res => {
        this.isShowcarrier= res.data.content
      }
      );
       this.getcarrier();
  },
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
    @import "src/styles/common.scss";

</style>